package com.itany.book.service.impl;

import com.itany.book.service.IBookService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@com.alibaba.dubbo.config.annotation.Service
public class BookServiceImpl implements IBookService{

    @Autowired
    private HttpSolrClient solrClient;

    @Override
    public void addDoc() {
        //需要两表关联将书籍与出版社名称等数据存入solr
        try{
            for(int i=0;i<10;i++){
                SolrInputDocument document = new SolrInputDocument();
                document.addField("id",i+1);
                document.addField("username","用户"+i);
                document.addField("password","234");
                solrClient.add(document);
            }
             /* solrClient.deleteById("1");
              solrClient.deleteByQuery("*:*");*/
            solrClient.commit();
        }catch (Exception e){
            e.printStackTrace();
            try {
                solrClient.rollback();
            }catch (Exception e1){
                e1.printStackTrace();
            }
        }
    }

    @Override
    public List<Map<String, String>> findDoc(String keyWord) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        try {
            //通过关键字生成查询语句
            String q="*:*";
            if(keyWord!=null && !"".equals(keyWord)){
                q="username:"+keyWord+" OR "+"password:"+keyWord;
            }
            //声明查询请求对象
            SolrQuery query = new SolrQuery();
            query.setQuery(q);
            //是否开启高亮
            query.setHighlight(true);
            //设置高亮标签
            query.setHighlightSimplePre("<font color='red'>");
            query.setHighlightSimplePost("</font>");
            //设置参与高亮的列
            query.setParam("hl.fl","username");
            //分页设置
          /*  query.setStart((pageNo-1)*pageSize);
            query.setRows(pageSize);*/
            //执行查询
            QueryResponse response = solrClient.query(query);
            //获取结果集对象
            SolrDocumentList results = response.getResults();
            //获取总数据条数
            System.out.println("总数据条数"+results.getNumFound());
            //循环设置高亮数据
            for(int i=0;i<results.size();i++){
                Map<String, String> vo = new HashMap<String, String>();
                SolrDocument sdoc = results.get(i);
                String id = (String)sdoc.get("id");
                //获取高亮后的数据
                Map<String,List<String>> map = response.getHighlighting().get(id);
                List<String> names = map.get("username");
                if(names!=null){
                    vo.put("username",names.get(0));
                }else{
                    vo.put("username",(String)sdoc.get("username"));
                }


                List<String> passwds = map.get("password");
                if(passwds!=null){
                    vo.put("password",passwds.get(0));
                }else{
                    vo.put("password",(String)sdoc.get("password"));
                }

                list.add(vo);
            }


        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

}
